﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Web;

namespace MarsExplorerWebSite
{
    /// <summary>
    /// Summary description for MarsExplorer
    /// </summary>
    public class MarsExplorer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            Assembly myAssembly = Assembly.GetExecutingAssembly();
            String s="";
            foreach (Type type in myAssembly.GetTypes())
            {
                Debug.WriteLine(type.FullName);
            }
            context.RewritePath(context.Server.UrlDecode(context.Request.Url.PathAndQuery));
            NameValueCollection a = context.Request.QueryString;
            if (a.Count > 0)
            {
                Type calledType = Type.GetType("MarsExplorerWebSite.Functions." + a.GetValues("function").First(), false, true);

                object o = Activator.CreateInstance(calledType, new Object[] { context });

                // Invoke the method itself. The string returned by the method winds up in s.
                // Note that stringParam is passed via the last parameter of InvokeMember,
                // as an array of Objects.
                try
                {
                    s = (String)calledType.InvokeMember("Execute",
                                       BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
                                       null,
                                       o,
                                       null);
                }
                catch (Exception e)
                {

                    s = e.ToString();
                }

            }
            else
            {

                string x = context.Request.Url.Scheme + "://" + context.Request.Url.Host + context.Request.Url.AbsolutePath;
                //string x = context.Request.Url.ToString();


                s += "----------------------PLAYER-------------------------------";
                s += "\r\n";
                s += x + "?Function=addPlayerToTeam&ipadid=xxxxxxxx&name=ze&teamid=1&playertype=1";
                s += "\r\n";
                s += x + "?Function=removePlayerFromTeam&ipadid=xxxxxxxx&teamid=1";
                s += "\r\n";
                s += "----------------------RESOURCES-------------------------------";
                s += "\r\n";
                s += x + "?Function=addResourcesToTeam&teamid=1&agua=1&Hidrogenio=1&Comida=1&Oxigenio=1&KitMedico=0&Previsao=0&Escudo=0&Fato=5&credito=5000";
                s += "\r\n";
                s += x + "?Function=removeResourcesFromTeam&teamid=1&agua=1&Hidrogenio=1&Comida=1&Oxigenio=1&KitMedico=0&Previsao=0&Escudo=0&Fato=5&credito=5000";
                s += "\r\n";
                s += x + "?Function=getAllResourcesFromTeam&teamid=1";
                s += "\r\n";
                s += x + "?Function=addResourcesToTeamTurn&teamid=1&agua=1&Hidrogenio=1&Comida=1&Oxigenio=1&KitMedico=0&Previsao=0&Escudo=0&Fato=5&credito=0";
                s += "\r\n";
                s += "----------------------TURN-------------------------------";
                s += "\r\n";
                s += x + "?Function=resetTeamTurn&teamid=1";
                s += "\r\n";
                s += x + "?Function=getTeamTurn&teamid=1";
                s += "\r\n";
                s += x + "?Function=setTurn&turnid=5";
                s += "\r\n";
                s += x + "?Function=getTurn";
                s += "\r\n";
                s += "----------------------SHIP POSITION-------------------------------";
                s += "\r\n";

                s += x + "?Function=moveTeamShip&teamid=1&positionid=5";
                s += "\r\n";
                s += x + "?Function=getTeamShipPosition&teamid=1";
                s += "\r\n";
                s += "----------------------TEAM-------------------------------";
                s += "\r\n";
                s += x + "?Function=setTeamMining&teamid=1";
                s += "\r\n";
                s += x + "?Function=addTeam&teamid=1";
                s += "\r\n";
                s += x + "?Function=getTeam&teamid=1";
                s += "\r\n";
                s += x + "?Function=getAllTeams";
                s += "\r\n";
                s += x + "?Function=getPlayersByTeam&teamid=1";
                s += "\r\n";
                s += x + "?Function=removeTeam";
                s += "\r\n";
                s += x + "?Function=getWeather&turnid=5";
                s += "\r\n";
                s += x + "?Function=getAllWeather";
                s += "\r\n";
                s += x + "?Function=resetGame";
                s += "\r\n";
                s += x + "?Function=getGameStatus";
                s += "\r\n";
               

               
              


            }


            context.Response.ContentType = "text/plain";
            context.Response.Write(s);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}